// fpTime.cpp: implementation of the fpTime class.
//
//////////////////////////////////////////////////////////////////////

/*

17/01/00 jd bug 12804 & 15946 don't allow null
22/03/00 jd bug 21364 time field weirdness

*/

#include "stdafx.h"
#include "fpTime.h"
#include "fpTimePeer.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

#ifdef FPCONTROLS
#ifndef _UNICODE

IMPLEMENT_DYNAMIC(fpTime, AWTComponent)

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

fpTime::fpTime()
{
	construct(CString());
}

fpTime::fpTime(CString& text)
{
	construct(text);
}

fpTime::~fpTime()
{
	if (m_actionListener != NULL)
	{
		if (m_actionListener->isMultiCaster())
		{
			AWTEventMulticaster* mc = dynamic_cast<AWTEventMulticaster*>(m_actionListener);
			delete mc;
		}
	}
}

CWnd* fpTime::createComponent()					 
{
/*
	CWnd* wParent = getParent()->getPeer();
	if (wParent == NULL)
		return wParent;
	fpTimePeer* pwnd = new fpTimePeer(this);
	DWORD dwStyle = WS_CHILD | WS_VISIBLE;
	pwnd->Create(dwStyle, getBounds(), wParent, 0);
	pwnd->SetFont(CFont::FromHandle(m_font));
	pwnd->SetMarginTop(0);
	pwnd->SetMarginLeft(1);
	pwnd->SetAppearance(2);
	pwnd->SetButtonStyle(2);
	pwnd->SetButtonDefaultAction(TRUE);
	pwnd->SetDateTimeFormat(5);
	pwnd->SetTimeStyle(2);
	pwnd->SetUserEntry(0);
	pwnd->SetAllowNull(TRUE);
	pwnd->SetUserDefinedFormat("hh:mm");
	pwnd->SetText(m_text.GetBuffer(0));
	return pwnd;
*/

	CWnd* wParent = getParent()->getPeer();
	if (wParent == NULL)
		return wParent;
	fpTimePeer* pwnd = new fpTimePeer(this);
	DWORD dwStyle = WS_CHILD | WS_VISIBLE;
	pwnd->Create(dwStyle, getBounds(), wParent, 0);
	pwnd->SetFont(CFont::FromHandle(m_font));
	pwnd->SetMarginTop(0);
	pwnd->SetMarginLeft(1);
	pwnd->SetAppearance(2);
	pwnd->SetButtonStyle(2);
	pwnd->SetButtonDefaultAction(TRUE);
//	pwnd->SetDateTimeFormat(4);
	pwnd->SetDateTimeFormat(5); //jd 22/3/00 bugref 21364
	pwnd->SetPopUpType(2); //jd 22/3/00 bugref 21364
	pwnd->SetUserDefinedFormat(_T("hh:nn")); //jd 22/3/00 bugref 21364
	pwnd->SetUserEntry(0);
//	pwnd->SetAllowNull(TRUE);  jd 17/1/00 bugref 12804 & 15946
	pwnd->SetText(m_text.GetBuffer(0));
	AWTColor* c = getForeground();
	if (c != NULL)
		pwnd->SetForeColor(c->getColorRef());
	if (!m_enabled)
		pwnd->EnableWindow(FALSE);
	return pwnd;
}

CSize fpTime::preferredSize(int columns) 
{
	if (m_peer == NULL)
		return CSize(0,0);
	CSize sz(0,0);
	fpTimePeer* pwnd = (fpTimePeer*)m_peer;
	CDC* pdc = pwnd->GetDC();
	CFont* ft = CFont::FromHandle(m_font);
	CFont* ftOld = pdc->SelectObject(ft);
	TEXTMETRIC tm;
	pdc->GetTextMetrics(&tm);
	pdc->SelectObject(ftOld);
	pwnd->ReleaseDC(pdc);	
	sz.cx = (tm.tmAveCharWidth * columns) + 32;
	sz.cy = tm.tmHeight + 6;
	return sz;
}

CSize fpTime::preferredSize() 
{
	if (m_peer == NULL)
		return CSize(0,0);
	return preferredSize(m_columns);
}

CSize fpTime::minimumSize(int columns) 
{
	if (m_peer == NULL)
		return CSize(0,0);
	CSize sz(0,0);
	fpTimePeer* pwnd = (fpTimePeer*)m_peer;
	CDC* pdc = pwnd->GetDC();
	CFont* ft = CFont::FromHandle(m_font);
	CFont* ftOld = pdc->SelectObject(ft);
	TEXTMETRIC tm;
	pdc->GetTextMetrics(&tm);
	pdc->SelectObject(ftOld);
	pwnd->ReleaseDC(pdc);	
	sz.cx = (tm.tmAveCharWidth * columns) + 32;
	sz.cy = tm.tmHeight + 6;
	return sz;
}

CSize fpTime::minimumSize() 
{
	if (m_peer == NULL)
		return CSize(0,0);
	return minimumSize(m_columns);
}

void fpTime::setColumns(int columns)
{
	if (columns < 0) return;
	if (m_columns != columns)
	{
		m_columns = columns;
		invalidate();
	}
}

void fpTime::setText(CString& text)
{
	m_text = text;
	if (m_peer != NULL)
	{	
		((fpTimePeer*)m_peer)->SetText(m_text.GetBuffer(0));	
	}
}

void fpTime::setText(LPTSTR text)
{
	if (text == NULL)
		m_text = CString();
	else
		m_text = text;
	if (m_peer != NULL)
		((fpTimePeer*)m_peer)->SetText(text);
}

CString& fpTime::getText()
{
	if (m_peer != NULL)
	{
		TCHAR sz[256];
		((fpTimePeer*)m_peer)->GetText(sz, sizeof(sz));
		m_text = sz;
	}
	return m_text;
}

void fpTime::setForeground(AWTColor* c)
{
	AWTComponent::setForeground(c);
	if (m_peer != NULL)
		((fpTimePeer*)m_peer)->SetForeColor(c->getColorRef());
}

#endif
#endif //FPCONTROLD